package top.zhangjianyong.utils;

import top.zhangjianyong.entity.Word;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： zhangjianyong
 * @date ： 2022/8/22 9:47 AM
 */
public class SplitWordUtil {


    private static final String UNUSED_WORDS = " \t\r\n、，。：；“‘”【】『』|=+-——（）*&……%￥#@！~·《》？/?<>,.;:'\"[]{}_)(^$!`";
    private static final String UNUSED_ENGLISH = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    public static String clean(String source) {

        for (char c : UNUSED_WORDS.toCharArray()) {
            source = source.replace(c + "", "");
        }

        for (char c : UNUSED_ENGLISH.toCharArray()) {
            source = source.replace(c + "", "");
        }

        return source;
    }


    public static Map<String, Word> splitWords(String source, int num) {
        int len = source.length();
        int part = len / 100;
        part = part == 0 ? 1 : part;
        Map<String, Word> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j <= i + num; j++) {
                if (j > len) {
                    break;
                }
                String key = source.substring(i, j);
                Word word = map.getOrDefault(key, new Word(key, len));
                word.add();
                if (i != 0 && key.length()>1) {
                    word.left(source.substring(i - 1, i));
                }
                if (j != len&& key.length()>1) {
                    word.right(source.substring(j, j + 1));
                }
                map.put(key, word);
            }

            if (i % part == part - 1) {
                System.out.println("完成 :" + ((double) i / len * 100) + "%");
            }
        }
        return map;
    }

    public static void stoneAndFree(Map<String, Word> wordsMap) {
        List<Word> collect = wordsMap.values().stream().filter(word -> word.getValue().length() > 1)
                .collect(Collectors.toList());
        int size = collect.size();
        int part = size / 100;
        part = part == 0 ? 1 : part;
        for (int i = 0; i < size; i++) {
            Word word = collect.get(i);
            String k = word.getValue();
            //计算凝固度
            if (k.length() > 1) {
                //左凝固程度
                String a = k.substring(0, 1);
                String b = k.substring(1);
                Double aFrequency = wordsMap.get(a).getFrequency();
                Double bFrequency = wordsMap.get(b).getFrequency();
                Double left = word.getFrequency() / (aFrequency * bFrequency);

                //右凝固程度
                String c = k.substring(0, k.length() - 1);
                String d = k.substring(k.length() - 1);
                Double cFrequency = wordsMap.get(c).getFrequency();
                Double dFrequency = wordsMap.get(d).getFrequency();
                Double right = word.getFrequency() / (cFrequency * dFrequency);
                word.stone(left, right);

                //计算自由度
                word.free();
            }
            if (i % part == part - 1) {
                System.out.println("完成 :" + ((double) i / size * 100) + "%");
            }
        }
    }


}
